package cn.itcalvin.sncoal.cth.component;

import cn.itcalvin.sncoal.cth.domain.ConfigSystem;
import cn.itcalvin.sncoal.cth.domain.ConfigSystemZb;
import cn.itcalvin.sncoal.cth.domain.ConfigSystemZbDetails;
import cn.itcalvin.sncoal.cth.domain.ConfigSystemZbDetailsExt;
import cn.itcalvin.sncoal.cth.pojo.response.config.RespConfigZb;
import cn.itcalvin.sncoal.cth.pojo.response.config.RespConfigZbAndDetails;
import cn.itcalvin.sncoal.cth.service.ConfigSystemService;
import lombok.Data;
import lombok.val;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 考核指标的缓存内
 */
@Data
@Component
public class ConfigCacheUtils {

    public static final List<Long> zbIdList60= Arrays.asList(5L,6L,7L,8L,9L,10L,11L,48L,51L,59L,60L,61L,78L,85L,100L,110L);
    public static final List<Long> zbIdList100= Arrays.asList(90L,91L);

    @Resource
    private ConfigSystemService configSystemService;

    private static ConfigSystemService CONFIG_SYSTEM_SERVICE;

    @PostConstruct
    public void init(){
        CONFIG_SYSTEM_SERVICE = configSystemService;
    }

    private List<ConfigSystem> listSystem = null;
    private Map<Long, List<ConfigSystemZb>> listSystemZb = null;
    private Map<Long, List<ConfigSystemZbDetails>>  listSystemZbDetailsList = null;
    private Map<Long, List<ConfigSystemZbDetailsExt>>  listSystemZbDetailsExtList = null;
    private static ConfigCacheUtils INSTANCE = null;

    /**
     * 获取单例
     * @return
     */
    public static ConfigCacheUtils getInstance() {
        if(INSTANCE==null){
            INSTANCE=new ConfigCacheUtils();
        }
        return INSTANCE;
    }

    /**
     * 加载指标到缓存
     */
    public void initCache() {
        this.setListSystem(CONFIG_SYSTEM_SERVICE.listSystem());
        this.setListSystemZb(CONFIG_SYSTEM_SERVICE.listSystemZb());
        this.setListSystemZbDetailsList(CONFIG_SYSTEM_SERVICE.listSystemZbDetails());
        this.setListSystemZbDetailsExtList(CONFIG_SYSTEM_SERVICE.listSystemZbDetailsExt());
    }


    public List<ConfigSystemZb> getAllZbByMineId(Long mineId){
        List<ConfigSystemZb> resultList=new ArrayList<>();
        listSystemZb.forEach((k,v)->{
            v.forEach(x->{
                if(x.getMineId()==null || x.getMineId().equals(mineId)){
                    resultList.add(x);
                }
            });
        });
        return resultList;
    }

    public List<ConfigSystemZbDetails> getAllZbDetailsByMineId(Long mineId){
        List<ConfigSystemZbDetails> resultList=new ArrayList<>();
        listSystemZbDetailsList.forEach((k,v)->{
            v.forEach(x->{
                if(x.getMineId()==null || x.getMineId().equals(mineId)){
                    resultList.add(x);
                }
            });
        });
        return resultList;
    }


    /**
     * 获取所有的系统
     * @return
     */
    public List<ConfigSystem> getAllSystemList(){
        if(listSystem==null){
            this.setListSystem(CONFIG_SYSTEM_SERVICE.listSystem());
        }
        return listSystem;
    }
    public ConfigSystem getSystemById(Long systemId){
        if(listSystem==null){
            this.setListSystem(CONFIG_SYSTEM_SERVICE.listSystem());
        }
        return listSystem.stream().filter(x -> x.getId().equals(systemId)).findFirst().orElse(null);
    }


    /**
     * 根据系统ID获取系统下的指标
     * @param systemId
     * @return
     */
    public List<ConfigSystemZb> getBySystemId(Long systemId){
        if(listSystemZb==null){
            this.setListSystemZb(CONFIG_SYSTEM_SERVICE.listSystemZb());
        }
        return listSystemZb.getOrDefault(systemId, null);
    }

    /**
     * 根据系统指标ID获取系统指标明细
     * @param systemZbId
     * @return
     */
    public List<ConfigSystemZbDetails> getBySystemZbId(Long systemZbId){
        if(listSystemZbDetailsList==null){
            this.setListSystemZbDetailsList(CONFIG_SYSTEM_SERVICE.listSystemZbDetails());
        }
        return listSystemZbDetailsList.getOrDefault(systemZbId, null);
    }

    /**
     * 修改指标明细溯源方法
     * @param configSystemZbDetailsExt
     */
    public void modifyZbDetailsFunction(ConfigSystemZbDetailsExt configSystemZbDetailsExt){
        if(listSystemZbDetailsExtList==null){
            this.setListSystemZbDetailsExtList(CONFIG_SYSTEM_SERVICE.listSystemZbDetailsExt());
        }
        listSystemZbDetailsExtList.get(configSystemZbDetailsExt.getSystemZbDetailsId()).stream().filter(configSystemZbDetails1 -> configSystemZbDetails1.getId().equals(configSystemZbDetailsExt.getId())).findFirst().ifPresent(configSystemZbDetails1 -> configSystemZbDetails1.setZbDetailsFunction(configSystemZbDetailsExt.getZbDetailsFunction()));
    }

    /**
     * 根据系统ID获取系统指标明细
     * @param systemId
     * @param mineId
     * @return
     */
    public List<RespConfigZb> getZbDetailsBySystemId(Long systemId,Long mineId) {
        final val configSystemZbs = listSystemZb.get(systemId);
        final val collect = configSystemZbs.stream()
                .filter(x ->
                        (x.getMineId() == null || x.getMineId().equals(mineId))
                ).collect(Collectors.toList());


        List<RespConfigZb> list=new ArrayList<>();
        for( final ConfigSystemZb configSystemZb :collect){
            if(!listSystemZbDetailsList.containsKey(configSystemZb.getId())){
                continue;
            }
            final val configSystemZbDetails = listSystemZbDetailsList.get(configSystemZb.getId()).stream()
                    .filter(x -> x.getMineId() == null || x.getMineId().equals(mineId)).collect(Collectors.toList());
            final val respConfigZb = new RespConfigZb();
            respConfigZb.setContent(configSystemZb.getContent());
            respConfigZb.setContentDesc(configSystemZb.getContentDesc());
            respConfigZb.setId(configSystemZb.getId());
            respConfigZb.setMineId(configSystemZb.getMineId());
            respConfigZb.setPfMethod(configSystemZb.getPfMethod());
            respConfigZb.setState(configSystemZb.getState());
            respConfigZb.setSystemId(configSystemZb.getSystemId());
            respConfigZb.setZbContent(configSystemZb.getZbContent());
            respConfigZb.setZbTotalScore(configSystemZb.getZbTotalScore());
            List<RespConfigZbAndDetails> tempList=new ArrayList<>();

            for (final ConfigSystemZbDetails x : configSystemZbDetails){
                final val respConfigZbAndDetails = new RespConfigZbAndDetails();
                respConfigZbAndDetails.setId(x.getId());
                respConfigZbAndDetails.setMineId(x.getMineId());
                respConfigZbAndDetails.setSystemId(x.getSystemId());
                respConfigZbAndDetails.setSystemZbId(x.getSystemZbId());
                respConfigZbAndDetails.setZbDetails(x.getZbDetails());
                respConfigZbAndDetails.setZbDetailsType(x.getZbDetailsType());
                respConfigZbAndDetails.setZbDetailsUnit(x.getZbDetailsUnit());
                respConfigZbAndDetails.setZbDetailsRemark(x.getZbDetailsRemark());
                if(listSystemZbDetailsExtList.containsKey(x.getId())){
                    final val first = listSystemZbDetailsExtList.get(x.getId()).stream()
                            .filter(y -> y.getMineId().equals(mineId)).findFirst();
                    if (first.isPresent()) {
                        respConfigZbAndDetails.setZbDetailsFunction(first.get().getZbDetailsFunction());
                        respConfigZbAndDetails.setZbDetailsIsAuto(first.get().getZbDetailsIsAuto());
                        respConfigZbAndDetails.setZbDetailsIsAutoRemark(first.get().getZbDetailsIsAutoRemark());
                    }
                }
                tempList.add(respConfigZbAndDetails);
            }
            respConfigZb.setZbDetailsList(tempList);
            list.add(respConfigZb);
        }
        return list;
    }
}
